package com.wyiot.pet.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyiot.core.api.ValidateGroup;
import com.wyiot.core.exception.ValidateException;
import com.wyiot.pet.model.dto.OrderDTO;
import com.wyiot.pet.model.dto.OrderQueryDTO;
import com.wyiot.pet.model.entity.OrderDO;
import com.wyiot.pet.model.vo.OrderDetailVO;
import com.wyiot.core.security.UserContext;
import com.wyiot.pet.service.OrderService;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 订单管理控制层
 *
 * @author yly
 * @since 2022年03月31日
 */
@RestController
public class OrderController {

    @Resource
    OrderService orderService;

    /**
     * 创建订单
     *
     * @see OrderDTO
     */
    @PostMapping("/order")
    public void createOrder(@RequestBody @Validated({ValidateGroup.Second.class}) OrderDTO orderDTO, BindingResult result) {
        if (!UserContext.getUserId().equals(orderDTO.getUserId())) {
            throw ValidateException.of("操作失败");
        }
        orderService.creatOrder(orderDTO);
    }

    /**
     * 取消订单
     *
     * @param orderId 订单id
     */
    @PutMapping("/order/{id}")
    public void cancelOrder(@PathVariable("id") Long orderId) {
        OrderDO orderDO = orderService.getById(orderId);
        if (orderId == null || !orderDO.getUserId().equals(UserContext.getUserId())) {
            throw ValidateException.of("操作失败");
        }
        orderService.cancelOrderByOrderId(orderId);
    }

    /**
     * 修改订单
     *
     * @see OrderDTO
     */
    @PutMapping("/order")
    public void updateOrder(@RequestBody @Validated({ValidateGroup.First.class}) OrderDTO orderDTO, BindingResult result) {
        if (orderDTO.getUserId() == null || !orderDTO.getUserId().equals(UserContext.getUserId())) {
            throw ValidateException.of("操作失败");
        }
        orderService.changeOrder(orderDTO);
    }


    /**
     * 查看订单详请
     *
     * @param orderId 订单id
     */
    @GetMapping("/orderdetail/{id}")
    public OrderDetailVO queryDetailOrderById(@PathVariable("id") Long orderId) {
        OrderDO orderDO = orderService.getById(orderId);
        if (Objects.isNull(orderDO.getOrderId()) || !orderDO.getUserId().equals(UserContext.getUserId())) {
            throw ValidateException.of("操作失败");
        }
        return orderService.queryOrderDetail(orderId);
    }

    /**
     * 支付接口
     *
     * @param orderId 订单id
     */
    @PutMapping("/payorder/{id}")
    public void payForOrder(@PathVariable("id") Long orderId) {
        OrderDO orderDO = orderService.getById(orderId);
        if (!orderDO.getUserId().equals(UserContext.getUserId())) {
            throw ValidateException.of("操作失败");
        }
        orderService.payOrder(orderId);
    }

    /**
     * 查询订单列表
     *
     * @see OrderQueryDTO
     */
    @GetMapping("/order/list")
    public Page<OrderDetailVO> queryOrderList(@RequestBody @Validated({ValidateGroup.Second.class}) OrderQueryDTO orderQueryDTO, BindingResult result) {
        if (orderQueryDTO.isIfMyTake()) {
            return orderService.querytakerOrders(orderQueryDTO, UserContext.getUserPhone());
        }
        return orderService.querysenderOrders(orderQueryDTO, UserContext.getUserId());
    }

    /**
     * 根据订单或运单号查询订单
     *
     * @see OrderQueryDTO
     */
    @GetMapping("/order")
    public OrderDO queryOrderByNumber(@RequestBody @Validated({ValidateGroup.First.class}) OrderQueryDTO orderQueryDTO, BindingResult result) {
        if (!orderQueryDTO.getUserId().equals(UserContext.getUserId())) {
            throw ValidateException.of("查询失败");
        }
        OrderDO orderDO = orderService.queryOrderByOrderNumber(orderQueryDTO.getNumber());
        if (orderDO !=null) {
            return orderDO;
        }
        return orderService.queryOrderByWaybillNumber(orderQueryDTO.getNumber());
    }
}